package com.dyz.swordFingerOffer;

import java.util.ArrayList;

public class LastRemaining_Solution62 {
    public int LastRemaining_Solution(int n, int m) {
        //取模

        if(n<1||m<1){return -1;}
        int last=0;
        for(int i=2;i<=n;i++){
            last=(last+m)%i;
        }
        return last;
    }
    public int LastRemaining_Solution1(int n, int m) {
        //取模
        //模拟小孩数
        ArrayList<Integer> list = new ArrayList<>();
        for (int i =0; i<n; i++){
            list.add(i);
        }
        int pre=0;
        while(list.size()>1){
            int cur = (pre+m-1)%list.size();//移除人的当前编号
            list.remove(cur);
            pre = cur;
        }
        return list.size() == 1?list.get(0):-1;
    }
}
